<template>
  <div>
    <van-overlay :show="isShow" @click="closeLogin">
      <div class="wrapper" @click.stop>
        <div class="main">
          <div class="title"> <van-icon @click="closeLogin" name="cross" /></div>
          <div class="login_username">
            <div class="login_icon">
              <img src="@/assets/images/icon_usr.png" alt="">
            </div>
            <input v-model="formData.username" class="username" type="text" placeholder="Please enter your account">
          </div>
          <div class="login_pwd">
            <div class="login_icon">
              <img src="@/assets/images/icon_password.png" alt="">
            </div>
            <input v-model="formData.code" class="password" type="text" placeholder="verification code">
            <img @click="getQrcodes" class="qrcode" :src="img" alt="">
          </div>
          <div class="login_button" @click="loginSubmit">
            <van-loading v-if="isLogin" style="margin-right: 10px" type="spinner" />
            <span v-else>Sign in</span>
          </div>
        </div>
      </div>
    </van-overlay>
  </div>
</template>

<script>
import {getQrcode, login} from "@/api/apiFox";
import {debounce} from "@/utils/evenNumbers";
import {Toast} from "vant";
export default {
  props:['loginShow'],
  data(){
    return{
      isLogin:false,
      img:'',
      isShow:false,
      formData:{
        username:'',
        code:'',
        uuid:''
      }
    }
  },
  methods:{
    closeLogin(){
      this.isLogin=false
      this.formData.username = this.formData.code=this.formData.uuid=''
      this.$emit('closeLogin',true)
      this.isShow=false
    },
    loginSubmit(){
      this.isLogin=true
      if (this.formData.username===''){
        Toast.fail('Username not filled in')
        this.isLogin=false
      }else if (this.formData.code === ''){
        Toast.fail('Verification code not filled in')
        this.isLogin=false
      }else{
        login(this.formData).then(res=>{
          localStorage.setItem('accessKey',res.token)
          sessionStorage.setItem('userInfo',res.id)
          this.$store.state.token = res.token
          this.closeLogin()
        }).catch(err=>{
          this.isLogin=false
          this.getQrcodes()
        })

      }
    },

    getQrcodes:debounce(function (){
      getQrcode().then(res=>{
        this.formData.uuid=res.uuid
        this.img = res.img
      })
    },500)
  },


  watch:{
    loginShow(newValue,oldValue){
      console.log(newValue)
      this.isShow = newValue
      if (newValue){
        localStorage.clear()
        sessionStorage.clear()
        this.getQrcodes()
      }
    }
  }
}
</script>

<style scoped lang="scss">
.wrapper {
  width: 100%;
  min-height: 100vh;
  background: none;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 0 5%;
  box-sizing: border-box;
  .main {
    width: 100%;
    min-height: 150px;
    background: #fff;
    padding: 3%;
    box-sizing: border-box;
    transform: translateY(-25%);
    border: 2vw solid #ffe69d;
    position: relative;
    border-radius: 10vw;
    display: flex;
    justify-content: center;
    flex-wrap: wrap;
    .title{
      width: 100%;
      font-size: 16px;
      color: #333333;
      text-indent: 15px;
      display: flex;
      justify-content: right;
    }
    .login_username{
      margin-top: 30px;
      width: 100%;
      height: 50px;
      //border-bottom: 1px solid #f5f5f5;
      display: flex;
      justify-content: space-around;
      align-items: center;
      .login_icon{
        width: 40px;
        height: 50px;
        display: flex;
        justify-content: center;
        align-items: center;
        img{
          width: 18px;
          height: 18px;
        }
      }
      .username{
        width: 100%;
        border-radius: 15px;
        box-shadow:rgba(149, 157, 165, 0.2) 0px 6px 25px;
        height: 50px;
        color: #233;
        font-size: 16px;
        padding: 0 0 0 20px;
        box-sizing: border-box;
        font-family: 'Fugaz One', sans-serif;
      }
      .username::placeholder{
        box-sizing: border-box;
        font-family: 'Fugaz One', sans-serif;
        color: #ddd;
        text-shadow:
            -1px -1px 0 #fff,
            1px -1px 0 #fff,
            -1px 1px 0 #fff,
            1px 1px 0 #fff; // 白色描边
      }
    }
    .login_pwd{
      margin-top: 25px;
      width: 100%;
      height: 50px;
      //border-bottom: 1px solid #f5f5f5;
      display: flex;
      justify-content: space-around;
      align-items: center;
      .login_icon{
        width: 40px;
        height: 50px;
        display: flex;
        justify-content: center;
        align-items: center;
        img{
          width: 18px;
          height: 18px;
        }
      }
      .password{
        width: 100%;
        border-radius: 15px;
        box-shadow:rgba(149, 157, 165, 0.2) 0px 6px 25px;
        height: 50px;
        color: #333;
        font-size: 16px;
        padding: 0 0 0 20px;
        font-family: 'Fugaz One', sans-serif;
        box-sizing: border-box;
      }
      .password::placeholder{
        box-sizing: border-box;
        font-family: 'Fugaz One', sans-serif;
        color: #ddd;
        text-shadow:
            -1px -1px 0 #fff,
            1px -1px 0 #fff,
            -1px 1px 0 #fff,
            1px 1px 0 #fff; // 白色描边
      }
      .qrcode{
        position: absolute;
        width: 70px;
        height: 35px;
        right: 30px;
      }
    }
    .login_button{
      margin-top: 50px;
      width: 200px;
      height: 50px;
      border-radius: 50px;
      //background-image: linear-gradient(109.6deg, #ecb844 29.9%, #ffdf7a 99.9%);
      //box-shadow: rgba(149, 157, 165, 0.5) 15px 8px 24px;
      background: url('@/assets/images/btn.png') no-repeat;
      background-size: 100% 100%;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 16px;
      font-family: 'Fugaz One', sans-serif;
      color: #ff8e0c;
      text-shadow:
          -1px -1px 0 #fff,
          1px -1px 0 #fff,
          -1px 1px 0 #fff,
          1px 1px 0 #fff; // 白色描边
      img{
        width: 18px;
        height: 18px;
        margin-right: 10px;
      }
    }
  }
}
</style>
